{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import QUANTAXIS as QA"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 我们模拟两个期货账户 \n",
    "\n",
    "1. acc 是允许保证金杠杆的账户 \n",
    "2. acc2 是不允许保证金杠杆的账户 allow_margin=False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "acc=QA.QA_Account(allow_sellopen=True,init_cash=10000,allow_t0=True,allow_margin=True,account_cookie='future_test',market_type=QA.MARKET_TYPE.FUTURE_CN,frequence=QA.FREQUENCE.FIFTEEN_MIN)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "acc2=QA.QA_Account(allow_sellopen=True,init_cash=10000,allow_t0=True,allow_margin=False,account_cookie='future_test',market_type=QA.MARKET_TYPE.FUTURE_CN,frequence=QA.FREQUENCE.FIFTEEN_MIN)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'buy_frozen_coeff': 0.09,\n",
       " 'commission_coeff_peramount': 0.0001,\n",
       " 'commission_coeff_pervol': 0.0,\n",
       " 'commission_coeff_today_peramount': 5e-05,\n",
       " 'commission_coeff_today_pervol': 0.0,\n",
       " 'exchange': 'SHFE',\n",
       " 'name': '螺纹钢',\n",
       " 'sell_frozen_coeff': 0.09,\n",
       " 'unit_table': 10}"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "acc.market_preset.get_code('RB1905')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "10"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "acc.market_preset.get_unit('RB1901')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "10"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "acc.market_preset.get_unit('M901')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.07"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "acc.market_preset.get_frozen('M901')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.09"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "acc.market_preset.get_frozen('RB1901')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [],
   "source": [
    "order=acc.send_order(code='RB1901', amount=1, time='2018-12-28 09:30:00', towards=QA.ORDER_DIRECTION.BUY_OPEN, price=3420, money=None, order_model=QA.ORDER_MODEL.MARKET, amount_model=QA.AMOUNT_MODEL.BY_AMOUNT)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "6921.2305"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "acc.cash_available"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [],
   "source": [
    "order2=acc2.send_order(code='RB1901', amount=1, time='2018-12-28 09:30:00', towards=QA.ORDER_DIRECTION.BUY_OPEN, price=3420, money=None, order_model=QA.ORDER_MODEL.MARKET, amount_model=QA.AMOUNT_MODEL.BY_AMOUNT)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "6579.1449999999995"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "acc2.cash_available"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 上面测试的是保证金账户的下单扣减"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "< QA_Order realorder_id Order_e1J6BwQ8 datetime:2018-12-28 09:30:00 code:RB1901 amount:1 price:3420 towards:2 btype:future_cn order_id:Order_e1J6BwQ8 account:future_test status:queued >"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "order"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "receive deal\n"
     ]
    }
   ],
   "source": [
    "order.trade('test_buyopen',3420,1,'2018-12-28 09:30:00')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>datetime</th>\n",
       "      <th>code</th>\n",
       "      <th>price</th>\n",
       "      <th>amount</th>\n",
       "      <th>cash</th>\n",
       "      <th>order_id</th>\n",
       "      <th>realorder_id</th>\n",
       "      <th>trade_id</th>\n",
       "      <th>account_cookie</th>\n",
       "      <th>commission</th>\n",
       "      <th>tax</th>\n",
       "      <th>message</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>2018-12-28 09:30:00</td>\n",
       "      <td>RB1901</td>\n",
       "      <td>3420.0</td>\n",
       "      <td>1</td>\n",
       "      <td>6918.58</td>\n",
       "      <td>Order_e1J6BwQ8</td>\n",
       "      <td>Order_e1J6BwQ8</td>\n",
       "      <td>test_buyopen</td>\n",
       "      <td>future_test</td>\n",
       "      <td>3.42</td>\n",
       "      <td>0</td>\n",
       "      <td>None</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "              datetime    code   price  amount     cash        order_id  \\\n",
       "0  2018-12-28 09:30:00  RB1901  3420.0       1  6918.58  Order_e1J6BwQ8   \n",
       "\n",
       "     realorder_id      trade_id account_cookie  commission  tax message  \n",
       "0  Order_e1J6BwQ8  test_buyopen    future_test        3.42    0    None  "
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "acc.history_table"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "3078.0"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "3420*10*0.09"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "3081.42"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "3078.0+3.420\t"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "10000.0"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "6918.58+3081.42"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'RB1901': {2: {'money': 3078.0, 'amount': 1}, -2: {'money': 0, 'amount': 0}}}"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "acc.frozen  #买入开仓 冻结的是买入的"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [],
   "source": [
    "order_sellclose=acc.send_order(code='RB1901', amount=1, time='2018-12-28 09:45:00', towards=QA.ORDER_DIRECTION.SELL_CLOSE, price=3425, money=None, order_model=QA.ORDER_MODEL.MARKET, amount_model=QA.AMOUNT_MODEL.BY_AMOUNT)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "< QA_Order realorder_id Order_9ZDL8TdI datetime:2018-12-28 09:45:00 code:RB1901 amount:1 price:3425 towards:-3 btype:future_cn order_id:Order_9ZDL8TdI account:future_test status:queued >"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "order_sellclose"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "receive deal\n"
     ]
    }
   ],
   "source": [
    "order_sellclose.trade('test_sellclose',3425,1,'2018-12-28 09:45:00')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>datetime</th>\n",
       "      <th>code</th>\n",
       "      <th>price</th>\n",
       "      <th>amount</th>\n",
       "      <th>cash</th>\n",
       "      <th>order_id</th>\n",
       "      <th>realorder_id</th>\n",
       "      <th>trade_id</th>\n",
       "      <th>account_cookie</th>\n",
       "      <th>commission</th>\n",
       "      <th>tax</th>\n",
       "      <th>message</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>2018-12-28 09:30:00</td>\n",
       "      <td>RB1901</td>\n",
       "      <td>3420.0</td>\n",
       "      <td>1</td>\n",
       "      <td>6918.580</td>\n",
       "      <td>Order_e1J6BwQ8</td>\n",
       "      <td>Order_e1J6BwQ8</td>\n",
       "      <td>test_buyopen</td>\n",
       "      <td>future_test</td>\n",
       "      <td>3.420</td>\n",
       "      <td>0</td>\n",
       "      <td>None</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2018-12-28 09:45:00</td>\n",
       "      <td>RB1901</td>\n",
       "      <td>3425.0</td>\n",
       "      <td>-1</td>\n",
       "      <td>10043.155</td>\n",
       "      <td>Order_9ZDL8TdI</td>\n",
       "      <td>Order_9ZDL8TdI</td>\n",
       "      <td>test_sellclose</td>\n",
       "      <td>future_test</td>\n",
       "      <td>3.425</td>\n",
       "      <td>0</td>\n",
       "      <td>None</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "              datetime    code   price  amount       cash        order_id  \\\n",
       "0  2018-12-28 09:30:00  RB1901  3420.0       1   6918.580  Order_e1J6BwQ8   \n",
       "1  2018-12-28 09:45:00  RB1901  3425.0      -1  10043.155  Order_9ZDL8TdI   \n",
       "\n",
       "     realorder_id        trade_id account_cookie  commission  tax message  \n",
       "0  Order_e1J6BwQ8    test_buyopen    future_test       3.420    0    None  \n",
       "1  Order_9ZDL8TdI  test_sellclose    future_test       3.425    0    None  "
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "acc.history_table"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'RB1901': {2: {'money': 0, 'amount': 0}, -2: {'money': 0, 'amount': 0}}}"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "acc.frozen"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "3082.5"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "3425*10*0.09"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "3085.925"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "3082.5+3.425\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "9954.949999999999"
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "6918.58+3081.42+(3081.42-3085.925)*10"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "10043.155"
      ]
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "6918.58+3078.0+(3082.5-3078.0)/0.09-3.425"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[10000, 6918.58, 10043.155]"
      ]
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "acc.cash"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {},
   "outputs": [],
   "source": [
    "order_sellopen=acc.send_order(code='RB1901', amount=1, time='2018-12-28 09:55:00', towards=QA.ORDER_DIRECTION.SELL_OPEN, price=3435, money=None, order_model=QA.ORDER_MODEL.MARKET, amount_model=QA.AMOUNT_MODEL.BY_AMOUNT)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "receive deal\n"
     ]
    }
   ],
   "source": [
    "order_sellopen.trade('test_sellopen',3435,1,'2018-12-28 09:55:00')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'RB1901': {2: {'money': 0, 'amount': 0}, -2: {'money': 3091.5, 'amount': 1}}}"
      ]
     },
     "execution_count": 32,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "acc.frozen"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>datetime</th>\n",
       "      <th>code</th>\n",
       "      <th>price</th>\n",
       "      <th>amount</th>\n",
       "      <th>cash</th>\n",
       "      <th>order_id</th>\n",
       "      <th>realorder_id</th>\n",
       "      <th>trade_id</th>\n",
       "      <th>account_cookie</th>\n",
       "      <th>commission</th>\n",
       "      <th>tax</th>\n",
       "      <th>message</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>2018-12-28 09:30:00</td>\n",
       "      <td>RB1901</td>\n",
       "      <td>3420.0</td>\n",
       "      <td>1</td>\n",
       "      <td>6918.580</td>\n",
       "      <td>Order_e1J6BwQ8</td>\n",
       "      <td>Order_e1J6BwQ8</td>\n",
       "      <td>test_buyopen</td>\n",
       "      <td>future_test</td>\n",
       "      <td>3.420</td>\n",
       "      <td>0</td>\n",
       "      <td>None</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2018-12-28 09:45:00</td>\n",
       "      <td>RB1901</td>\n",
       "      <td>3425.0</td>\n",
       "      <td>-1</td>\n",
       "      <td>10043.155</td>\n",
       "      <td>Order_9ZDL8TdI</td>\n",
       "      <td>Order_9ZDL8TdI</td>\n",
       "      <td>test_sellclose</td>\n",
       "      <td>future_test</td>\n",
       "      <td>3.425</td>\n",
       "      <td>0</td>\n",
       "      <td>None</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>2018-12-28 09:55:00</td>\n",
       "      <td>RB1901</td>\n",
       "      <td>3435.0</td>\n",
       "      <td>-1</td>\n",
       "      <td>6948.220</td>\n",
       "      <td>Order_oFDKcyOr</td>\n",
       "      <td>Order_oFDKcyOr</td>\n",
       "      <td>test_sellopen</td>\n",
       "      <td>future_test</td>\n",
       "      <td>3.435</td>\n",
       "      <td>0</td>\n",
       "      <td>None</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "              datetime    code   price  amount       cash        order_id  \\\n",
       "0  2018-12-28 09:30:00  RB1901  3420.0       1   6918.580  Order_e1J6BwQ8   \n",
       "1  2018-12-28 09:45:00  RB1901  3425.0      -1  10043.155  Order_9ZDL8TdI   \n",
       "2  2018-12-28 09:55:00  RB1901  3435.0      -1   6948.220  Order_oFDKcyOr   \n",
       "\n",
       "     realorder_id        trade_id account_cookie  commission  tax message  \n",
       "0  Order_e1J6BwQ8    test_buyopen    future_test       3.420    0    None  \n",
       "1  Order_9ZDL8TdI  test_sellclose    future_test       3.425    0    None  \n",
       "2  Order_oFDKcyOr   test_sellopen    future_test       3.435    0    None  "
      ]
     },
     "execution_count": 33,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "acc.history_table"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "-1\n"
     ]
    }
   ],
   "source": [
    "order_buyclose=acc.send_order(code='RB1901', amount=1, time='2018-12-28 10:55:00', towards=QA.ORDER_DIRECTION.BUY_CLOSE, price=3420, money=None, order_model=QA.ORDER_MODEL.MARKET, amount_model=QA.AMOUNT_MODEL.BY_AMOUNT)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "receive deal\n"
     ]
    }
   ],
   "source": [
    "order_buyclose.trade('test_buyclose',3420,1,'2018-12-28 10:55:00')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'RB1901': {2: {'money': 0, 'amount': 0}, -2: {'money': 0, 'amount': 0}}}"
      ]
     },
     "execution_count": 36,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "acc.frozen"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>datetime</th>\n",
       "      <th>code</th>\n",
       "      <th>price</th>\n",
       "      <th>amount</th>\n",
       "      <th>cash</th>\n",
       "      <th>order_id</th>\n",
       "      <th>realorder_id</th>\n",
       "      <th>trade_id</th>\n",
       "      <th>account_cookie</th>\n",
       "      <th>commission</th>\n",
       "      <th>tax</th>\n",
       "      <th>message</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>2018-12-28 09:30:00</td>\n",
       "      <td>RB1901</td>\n",
       "      <td>3420.0</td>\n",
       "      <td>1</td>\n",
       "      <td>6918.580</td>\n",
       "      <td>Order_e1J6BwQ8</td>\n",
       "      <td>Order_e1J6BwQ8</td>\n",
       "      <td>test_buyopen</td>\n",
       "      <td>future_test</td>\n",
       "      <td>3.420</td>\n",
       "      <td>0</td>\n",
       "      <td>None</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2018-12-28 09:45:00</td>\n",
       "      <td>RB1901</td>\n",
       "      <td>3425.0</td>\n",
       "      <td>-1</td>\n",
       "      <td>10043.155</td>\n",
       "      <td>Order_9ZDL8TdI</td>\n",
       "      <td>Order_9ZDL8TdI</td>\n",
       "      <td>test_sellclose</td>\n",
       "      <td>future_test</td>\n",
       "      <td>3.425</td>\n",
       "      <td>0</td>\n",
       "      <td>None</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>2018-12-28 09:55:00</td>\n",
       "      <td>RB1901</td>\n",
       "      <td>3435.0</td>\n",
       "      <td>-1</td>\n",
       "      <td>6948.220</td>\n",
       "      <td>Order_oFDKcyOr</td>\n",
       "      <td>Order_oFDKcyOr</td>\n",
       "      <td>test_sellopen</td>\n",
       "      <td>future_test</td>\n",
       "      <td>3.435</td>\n",
       "      <td>0</td>\n",
       "      <td>None</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>2018-12-28 10:55:00</td>\n",
       "      <td>RB1901</td>\n",
       "      <td>3420.0</td>\n",
       "      <td>1</td>\n",
       "      <td>10186.300</td>\n",
       "      <td>Order_fwdWsHOD</td>\n",
       "      <td>Order_fwdWsHOD</td>\n",
       "      <td>test_buyclose</td>\n",
       "      <td>future_test</td>\n",
       "      <td>3.420</td>\n",
       "      <td>0</td>\n",
       "      <td>None</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "              datetime    code   price  amount       cash        order_id  \\\n",
       "0  2018-12-28 09:30:00  RB1901  3420.0       1   6918.580  Order_e1J6BwQ8   \n",
       "1  2018-12-28 09:45:00  RB1901  3425.0      -1  10043.155  Order_9ZDL8TdI   \n",
       "2  2018-12-28 09:55:00  RB1901  3435.0      -1   6948.220  Order_oFDKcyOr   \n",
       "3  2018-12-28 10:55:00  RB1901  3420.0       1  10186.300  Order_fwdWsHOD   \n",
       "\n",
       "     realorder_id        trade_id account_cookie  commission  tax message  \n",
       "0  Order_e1J6BwQ8    test_buyopen    future_test       3.420    0    None  \n",
       "1  Order_9ZDL8TdI  test_sellclose    future_test       3.425    0    None  \n",
       "2  Order_oFDKcyOr   test_sellopen    future_test       3.435    0    None  \n",
       "3  Order_fwdWsHOD   test_buyclose    future_test       3.420    0    None  "
      ]
     },
     "execution_count": 37,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "acc.history_table"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 快速撮合接口的测试"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "metadata": {},
   "outputs": [],
   "source": [
    "acc.reset_assets(init_cash=10000)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "metadata": {},
   "outputs": [],
   "source": [
    "acc.receive_simpledeal(code='RB1901', trade_price=3420, trade_amount=1, trade_towards=QA.ORDER_DIRECTION.BUY_OPEN, trade_time='2018-12-28 09:30:00')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "metadata": {},
   "outputs": [],
   "source": [
    "acc.receive_simpledeal(code='RB1901', trade_price=3425, trade_amount=1, trade_towards=QA.ORDER_DIRECTION.SELL_CLOSE, trade_time='2018-12-28 09:45:00')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "metadata": {},
   "outputs": [],
   "source": [
    "acc.receive_simpledeal(code='RB1901', trade_price=3435, trade_amount=1, trade_towards=QA.ORDER_DIRECTION.SELL_OPEN, trade_time='2018-12-28 09:55:00')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "metadata": {},
   "outputs": [],
   "source": [
    "acc.receive_simpledeal(code='RB1901', trade_price=3420, trade_amount=1, trade_towards=QA.ORDER_DIRECTION.BUY_CLOSE, trade_time='2018-12-28 10:45:00')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>datetime</th>\n",
       "      <th>code</th>\n",
       "      <th>price</th>\n",
       "      <th>amount</th>\n",
       "      <th>cash</th>\n",
       "      <th>order_id</th>\n",
       "      <th>realorder_id</th>\n",
       "      <th>trade_id</th>\n",
       "      <th>account_cookie</th>\n",
       "      <th>commission</th>\n",
       "      <th>tax</th>\n",
       "      <th>message</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>2018-12-28 09:30:00</td>\n",
       "      <td>RB1901</td>\n",
       "      <td>3420</td>\n",
       "      <td>1</td>\n",
       "      <td>6918.580</td>\n",
       "      <td>None</td>\n",
       "      <td>None</td>\n",
       "      <td>None</td>\n",
       "      <td>future_test</td>\n",
       "      <td>3.420</td>\n",
       "      <td>0</td>\n",
       "      <td>None</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2018-12-28 09:45:00</td>\n",
       "      <td>RB1901</td>\n",
       "      <td>3425</td>\n",
       "      <td>-1</td>\n",
       "      <td>10043.155</td>\n",
       "      <td>None</td>\n",
       "      <td>None</td>\n",
       "      <td>None</td>\n",
       "      <td>future_test</td>\n",
       "      <td>3.425</td>\n",
       "      <td>0</td>\n",
       "      <td>None</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>2018-12-28 09:55:00</td>\n",
       "      <td>RB1901</td>\n",
       "      <td>3435</td>\n",
       "      <td>-1</td>\n",
       "      <td>6948.220</td>\n",
       "      <td>None</td>\n",
       "      <td>None</td>\n",
       "      <td>None</td>\n",
       "      <td>future_test</td>\n",
       "      <td>3.435</td>\n",
       "      <td>0</td>\n",
       "      <td>None</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>2018-12-28 10:45:00</td>\n",
       "      <td>RB1901</td>\n",
       "      <td>3420</td>\n",
       "      <td>1</td>\n",
       "      <td>10186.300</td>\n",
       "      <td>None</td>\n",
       "      <td>None</td>\n",
       "      <td>None</td>\n",
       "      <td>future_test</td>\n",
       "      <td>3.420</td>\n",
       "      <td>0</td>\n",
       "      <td>None</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "              datetime    code  price  amount       cash order_id  \\\n",
       "0  2018-12-28 09:30:00  RB1901   3420       1   6918.580     None   \n",
       "1  2018-12-28 09:45:00  RB1901   3425      -1  10043.155     None   \n",
       "2  2018-12-28 09:55:00  RB1901   3435      -1   6948.220     None   \n",
       "3  2018-12-28 10:45:00  RB1901   3420       1  10186.300     None   \n",
       "\n",
       "  realorder_id trade_id account_cookie  commission  tax message  \n",
       "0         None     None    future_test       3.420    0    None  \n",
       "1         None     None    future_test       3.425    0    None  \n",
       "2         None     None    future_test       3.435    0    None  \n",
       "3         None     None    future_test       3.420    0    None  "
      ]
     },
     "execution_count": 43,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "acc.history_table"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'RB1901': {2: {'money': 0, 'amount': 0}, -2: {'money': 0, 'amount': 0}}}"
      ]
     },
     "execution_count": 44,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "acc.frozen"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
